using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace CS490_Capstone___Stubby.Page
{
    public partial class SignUp : System.Web.UI.Page
    {
        WebServiceApplication.WebService webservice = new WebServiceApplication.WebService();

        String browserHeight;

        protected void Page_Load(object sender, EventArgs e)
        {
            int contentHeight;

            // read screen height passed through URL
            if (Request.QueryString.ToString() != "")
            {
                HtmlGenericControl searchBody = (HtmlGenericControl)Page.Master.FindControl("myBody");
                // displays the appropriate header menu
                searchBody.Attributes.Add("onload", "showJavaScriptHeader();");

                // sets browser height to the visible browser height as passed by the query string
                // sets browser height to minimum of 550px if browser height is smaller
                browserHeight = (int.Parse(Request.QueryString["height"].ToString()) > 550) 
                    ? Request.QueryString["height"].ToString() : "550";
            }
            else
            {
                // sets browser height to default 800px if browser height cannot be determined
                browserHeight = "800";
            }

            // calculate and set the size of the input panel to fill the screen
            content.Height = contentHeight = int.Parse(browserHeight) - 120;
            topSpacer.Height = ((contentHeight - 390) / 2) - 15 > 0 ? ((contentHeight - 390) / 2) - 15 : 0; 
        }

        private bool validInput()
        {
            // char arrays to test various inputs against
            char[] badChar = "*&#()[]{}%$@<>?/\\~`=!^|\":;+".ToCharArray();
            char[] badEmail = "*&#()[]{}%$<>?/\\~`=!^|\":;".ToCharArray();
            char[] badAddress = "*&()[]{}%$@<>?/\\~`=!^|\":;".ToCharArray();
            char[] badZip = "*&#()[]{}%$@<>?/\\~`=!^|\":;+".ToCharArray();
                badZip.Concat("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray());
            char[] goodChar = "`~!@#$%^&+=[]{}<>".ToCharArray();
            char[] badPassword = "*?".ToCharArray();
            char[] at = "@".ToCharArray();
            char[] dot = ".".ToCharArray();

            // change background color of all fields white
            firstName.BackColor = System.Drawing.Color.White;
            lastName.BackColor = System.Drawing.Color.White;
            address1.BackColor = System.Drawing.Color.White;
            address2.BackColor = System.Drawing.Color.White;
            city.BackColor = System.Drawing.Color.White;
            state.BackColor = System.Drawing.Color.White;
            zipCode.BackColor = System.Drawing.Color.White;
            userName.BackColor = System.Drawing.Color.White;
            email.BackColor = System.Drawing.Color.White;
            password.BackColor = System.Drawing.Color.White;
            password1.BackColor = System.Drawing.Color.White;
            
            // highlights fields that are invalid/incomplete
            if (firstName.Text == "" || firstName.Text.IndexOfAny(badChar) >= 0)
            {
                firstName.BackColor = System.Drawing.ColorTranslator.FromHtml("#FEF201");
            }
            if (lastName.Text == "" || lastName.Text.IndexOfAny(badChar) >= 0)
            {
                lastName.BackColor = System.Drawing.ColorTranslator.FromHtml("#FEF201");
            }
            if (address1.Text == "" || address1.Text.IndexOfAny(badAddress) >= 0)
            {
                address1.BackColor = System.Drawing.ColorTranslator.FromHtml("#FEF201");
            }
            if (city.Text == "" || city.Text.IndexOfAny(badAddress) >= 0)
            {
                city.BackColor = System.Drawing.ColorTranslator.FromHtml("#FEF201");
            }
            if (state.SelectedIndex <= 0)
            {
                state.BackColor = System.Drawing.ColorTranslator.FromHtml("#FEF201");
            }
            if (zipCode.Text == "" || zipCode.Text.IndexOfAny(badZip) >= 0 ||
                zipCode.Text.Length != 5)
            {
                zipCode.BackColor = System.Drawing.ColorTranslator.FromHtml("#FEF201");
            }
            if (userName.Text == "" || userName.Text.IndexOfAny(badChar) >= 0)
            {
                userName.BackColor = System.Drawing.ColorTranslator.FromHtml("#FEF201");
            }
            if (email.Text == "" || email.Text.IndexOfAny(badEmail) >= 0 ||
                email.Text.IndexOfAny(at) < 0 || email.Text.IndexOfAny(dot) < 0)
            {
                email.BackColor = System.Drawing.ColorTranslator.FromHtml("#FEF201");
            }
            if (password.Text == "" || password.Text.Length < 8 || password.Text.IndexOfAny(goodChar) < 0 ||
                password.Text.IndexOfAny(badPassword) >= 0)
            {
                password.BackColor = System.Drawing.ColorTranslator.FromHtml("#FEF201");
            }
            if (password.Text != password1.Text)
            {
                password1.BackColor = System.Drawing.ColorTranslator.FromHtml("#FEF201");
            }

            // checks to see if any fields are highlighted as invalid/incomplete
            if (firstName.BackColor.Equals(System.Drawing.ColorTranslator.FromHtml("#FEF201")) ||
                lastName.BackColor.Equals(System.Drawing.ColorTranslator.FromHtml("#FEF201")) ||
                address1.BackColor.Equals(System.Drawing.ColorTranslator.FromHtml("#FEF201")) ||
                address2.BackColor.Equals(System.Drawing.ColorTranslator.FromHtml("#FEF201")) ||
                city.BackColor.Equals(System.Drawing.ColorTranslator.FromHtml("#FEF201")) ||
                state.BackColor.Equals(System.Drawing.ColorTranslator.FromHtml("#FEF201")) ||
                zipCode.BackColor.Equals(System.Drawing.ColorTranslator.FromHtml("#FEF201")) ||
                userName.BackColor.Equals(System.Drawing.ColorTranslator.FromHtml("#FEF201")) ||
                email.BackColor.Equals(System.Drawing.ColorTranslator.FromHtml("#FEF201")) ||
                password.BackColor.Equals(System.Drawing.ColorTranslator.FromHtml("#FEF201")) ||
                password1.BackColor.Equals(System.Drawing.ColorTranslator.FromHtml("#FEF201")))
            // invalid/incomplete input detected
            {
                return false;
            }
            else
            // no invalid/incomplete characters detected
            {
                return true;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!validInput())
            {
                // do nothing other than highlight which fields are incomplete or invalid
            }
            else
            // beginning of the valid input block -- do what the button is supposed to do
            {
                // create an account for a new user if passwords match
                webservice.insertUser(firstName.Text, lastName.Text, email.Text, newsletterStatus.Checked, 
                    DateTime.Now, userName.Text, password.Text, 0, true, false, address1.Text, 
                    address2.Text, city.Text, state.Text, zipCode.Text);

                // switches from the input to the output tables so user can navigate back to Search
                input.Visible = false;
                output.Visible = true;
            } // close of the valid input block
        }
    }
}